(function(){"use strict";BX.namespace("BX.Landing");var a=BX.Landing.Utils.isPlainObject;var t=BX.Landing.Utils.isString;var n=BX.Landing.Utils.addQueryParams;BX.Landing.Backend=function(){this.ajaxController=n("/bitrix/tools/landing/ajax.php",{site:BX.message["SITE_ID"]?BX.message("SITE_ID"):undefined})};BX.Landing.Backend.instance=null;BX.Landing.Backend.getInstance=function(){if(!BX.Landing.Backend.instance){BX.Landing.Backend.instance=new BX.Landing.Backend}return BX.Landing.Backend.instance};BX.Landing.Backend.prototype={action:function(a,n,i,e){e=BX.type.isPlainObject(e)?e:{};i=BX.type.isPlainObject(i)?i:{};if(this.getSiteId()!==undefined){BX.Landing.Utils.assign(i,{site_id:this.getSiteId()})}var o={};o.sessid=BX.bitrix_sessid();o.action=a.replace("Landing\\Block","Block");o.data=typeof n==="object"?n:{};o.data.lid=o.data.lid||BX.Landing.Main.getInstance().id;if("action"in e){o.action=e.action}if("block"in e){o.data.block=e.block}if("lid"in e){o.data.lid=e.lid}if("id"in e){o.data.id=e.id}var r=BX.util.add_url_param(this.ajaxController,BX.util.objectMerge({action:o.action},i));return new Promise(function(a,t){var n=BX.Http.Data.convertObjectToFormData(o);var i=BX.ajax({method:"POST",dataType:"json",url:r,data:n,start:false,preparePost:false,onsuccess:function(n){if(!!n&&n.type==="error"){t(n)}else{a(n.result)}},onfailure:function(a){t(a)}});i.send(n)}).then(function(a){if(o.action==="Block::updateNodes"||o.action==="Block::removeCard"||o.action==="Block::cloneCard"||o.action==="Block::addCard"||o.action==="Block::updateStyles"){BX.Landing.UI.Panel.StatusPanel.getInstance().update()}return a}).catch(function(a){if(o.action!=="Block::getById"){a=t(a)?{type:"error"}:a;a.action=o.action;BX.Landing.ErrorManager.getInstance().add(a)}return Promise.reject()})},batch:function(a,n,i){i=BX.type.isPlainObject(i)?i:{};BX.Landing.Utils.assign(i,{site_id:n.siteId||this.getSiteId()});var e={};e.sessid=BX.bitrix_sessid();e.action=a.replace("Landing\\Block","Block");e.data={};e.batch=typeof n==="object"?n:{};e.data.lid=e.data.lid||BX.Landing.Main.getInstance().id;var o=BX.util.add_url_param(this.ajaxController,BX.util.objectMerge({action:e.action},i));return new Promise(function(a,t){var n=BX.Http.Data.convertObjectToFormData(e);var i=BX.ajax({method:"POST",dataType:"json",url:o,data:n,start:false,preparePost:false,onsuccess:function(n){if(!!n&&n.type==="error"){t(n)}else{a(n)}},onfailure:function(a){t(a)}});i.send(n)}).then(function(a){BX.Landing.UI.Panel.StatusPanel.getInstance().update();return a}).catch(function(a){if(e.action!=="Block::getById"){a=t(a)?{type:"error"}:a;a.action=e.action;BX.Landing.ErrorManager.getInstance().add(a)}return Promise.reject()})},getSiteId:function(){var a;try{a=BX.Landing.Main.getInstance().options.site_id}catch(t){a=-1}return a},upload:function(a,n){var i=new FormData;var e=n||{};var o="Block::uploadFile";i.append("sessid",BX.bitrix_sessid());i.append("picture",a,a.name);if("block"in e){i.append("data[block]",e.block)}if("lid"in e){o="Landing::uploadFile";i.append("data[lid]",e.lid);i.append("action",o)}if("id"in e){o="Site::uploadFile";i.append("data[id]",e.id);i.set("action",o)}var r=BX.util.add_url_param(this.ajaxController,{action:o,site_id:this.getSiteId()});if(e.context){r=BX.util.add_url_param(r,{context:e.context})}return new Promise(function(a,t){var n=BX.ajax({url:r,method:"POST",dataType:"json",data:i,start:false,preparePost:false,onsuccess:function(n){if(!!n&&n.type==="error"){t(n)}else{a(n.result)}},onfailure:function(a){t(a)}});n.send(i)}).catch(function(a){a=t(a)?{type:"error"}:a;a.action="Block::uploadFile";BX.Landing.ErrorManager.getInstance().add(a);return Promise.reject(a)})},uploadImage:function(n,i,e,o){if(!n){n=document.createElement("form")}o=a(o)?o:{};var r={};r.sessid=BX.bitrix_sessid();r.action="action"in o?o.action:"Utils::uploadFile";r.picture=i;r.data={};r.data.params=typeof e==="object"?e:{};if("block"in o){r.data.block=o.block}if("lid"in o){r.data.lid=o.lid}if("id"in o){r.data.id=o.id}var d=BX.util.add_url_param(this.ajaxController,{action:r.action,site_id:this.getSiteId()});return new Promise(function(a,t){BX.ajax.submitAjax(n,{url:d,method:"POST",dataType:"json",data:r,onsuccess:function(n){if(!!n&&n.type==="error"){t(n)}else{a(n.result)}},onfailure:function(a){t(a)}})}).catch(function(a){a=t(a)?{type:"error"}:a;a.action=r.action;BX.Landing.ErrorManager.getInstance().add(a);return Promise.reject()})}}})();
//# sourceMappingURL=backend.map.js